<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Publisher;
use App\Models\Member;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class PublisherController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Publisher(), function (Grid $grid) {
            $grid->model()->orderByDesc('created_at');
            $grid->column('created_at')->sortable();
            $grid->column('avatar')->image('', 50, 50);
            $grid->column('name');
            $grid->column('open_id');
            $grid->column('is_examine', '前台审核权限')->switch();

            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableDeleteButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('name')->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });

            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableBatchDelete();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Publisher(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('avatar');
            $show->field('open_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Publisher(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->image('avatar')->required()->autoUpload()->saveFullUrl()->uniqueName();
            $form->text('open_id');
            $form->radio('is_examine', '是否有前台审核权限')->options([0 => '否', 1 => '是'])->default(0);

            $form->display('created_at');
            $form->display('updated_at');
            $form->disableDeleteButton();
            $form->disableViewButton();

            $form->saving(function (Form $form) {
                $id = $form->getKey();
                $open_id = $form->open_id;
                $name = $form->name;
                $avatar = $form->avatar;
                $member = Member::where(['open_id' => $open_id])->first();

                if (\App\Models\Publisher::where('open_id', $open_id)->when($id, function ($q) use ($id) {
                    return $q->where('id', '<>', $id);
                })->exists()) {
                    return $form->response()->error('openid已存在,无需重复添加');
                }

                if (!$member && $open_id) {
                    return $form->response()->error('该用户不存在');
                } elseif ($member) {
                    $member->nickname = $name;
                    $member->avatar = $avatar;
                    $member->save();
                }
            });
        });
    }
}
